package com.mohunhun.controller;

import com.mohunhun.common.AppResult;
import com.mohunhun.common.ResultCode;
import com.mohunhun.exception.ApplicationException;
import com.mohunhun.model.Board;
import com.mohunhun.services.IBoardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@Tag(name = "板块接口")
@RequestMapping("/board")
public class BoardController {

    @Value("${forum.index.board-num:9}")
    private Integer indexBoarNum;

    @Resource
    private IBoardService boardService;


    /**
     * 获取首页板块列表
     *
     * @return 返回包含置顶列表数据的AppResult对象
     */
    @Operation(summary = "主页中显示的板块")
    @GetMapping("/topList")
    public AppResult<List<Board>> topList() {
        log.info("首页板块个数为：" + indexBoarNum);
        List<Board> boards = boardService.selectByNum(indexBoarNum);
        if (boards == null) {
            boards = new ArrayList<>();
        }


        return AppResult.success(boards);
    }


    @Operation(summary = "获取板块信息")
    @GetMapping("/getById")
    public AppResult<Board> getById(@Parameter(description = "板块id") @RequestParam("id") @NonNull Long id) {

        Board board = boardService.selectById(id);

        if (board == null || board.getDeleteState() == 1) {
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_EXISTS));
        }

        return AppResult.success(board);
    }

}
